const log4js = require('log4js');

const levels = {
  trace: log4js.levels.TRACE,
  debug: log4js.levels.DEBUG,
  info: log4js.levels.INFO,
  warn: log4js.levels.WARN,
  error: log4js.levels.ERROR,
  fatal: log4js.levels.FATAL,
};

// 配置日志规则
log4js.configure({
  appenders: {
    // 控制台打印
    console: { type: 'console' },
    // info 级别 放到 logs/all-logs.log
    info: {
      type: 'file',
      filename: 'logs/all-logs.log',
    },
    // error 级别 放到 logs/log.日期.log
    error: {
      type: 'dateFile',
      filename: 'logs/log',
      pattern: 'yyyy-MM-dd.log',
      alwaysIncludePattern: true, //设置文件名称为 filename+pattern
    },
  },
  categories: {
    default: { appenders: ['console'], level: 'debug' },
    info: {
      appenders: ['info', 'console'],
      level: 'info',
    },
    error: {
      appenders: ['error', 'console'],
      level: 'error',
    },
  },
});

/**
 * 分别设置 日志输出级别 为 info debug error
 * @params {string} content
 */
const alLevel = ['debug', 'info', 'error'];
alLevel.forEach((item) => {
  exports[item] = (content) => {
    let logger = log4js.getLogger(item);
    logger.level = levels[item];
    logger[item](content);
  };
});
